<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/11 15:40
 */

namespace App\Transformers;

use App\Models\MedicalFollowUp;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use League\Fractal\Manager;
use League\Fractal\TransformerAbstract;
use App\Models\MedicalHpiSub;
use App\Models\MedicalRecord;
use App\Models\MedicalDispose;

class PreviousFollowWithRecordTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['previousSub', 'previousMedication'];

    /**
     * 上次病历
     * @var \App\Models\MedicalRecord|null
     */
    protected $record;

    /**
     * 当前请求
     * @var \Illuminate\Http\Request
     */
    protected $request;

    /**
     * @var \League\Fractal\Manager
     */
    protected $manager;

    /**
     * 构造器
     * PreviousFollowWithRecordTransformer constructor.
     * @param \Illuminate\Http\Request $request
     * @param \League\Fractal\Manager  $manager
     * @param null                     $record_id
     */
    public function __construct(Request $request, Manager $manager, $record_id = null)
    {
        $this->request = $request;

        if (empty($record_id)) {
            $record_id = $request->input('record_id');
        }

        $this->manager = $manager;

        $this->setRecord($record_id);
    }

    /**
     * @param $record_id
     */
    public function setRecord($record_id)
    {
        $includes = $this->manager->getRequestedIncludes();

        if (array_intersect(['previousSub', 'previousMedication'], $includes)) {
            $this->record = (new MedicalRecord())->previous($record_id);
        }
    }

    /**
     * @param $array
     * @return array
     */
    public function transform($array) { return []; }

    /**
     * 获取上一次随诊或首诊的现病史组
     * @param $array
     * @return \League\Fractal\Resource\Collection
     */
    public function includePreviousSub($array)
    {
        $previousSub = [];

        if ($this->record) {
            $previousSub = MedicalHpiSub::where(['record_id' => $this->record->id])->get();
        }

        return $this->collection($previousSub, new HpiSubTransformer());
    }

    /**
     * 获取上一次随诊或首诊的处理 中西医 用药
     * @param $array
     * @return \League\Fractal\Resource\Item
     */
    public function includePreviousMedication($array)
    {
        $medication = [];
        $select = ['medication'];
        if ($this->record instanceof MedicalRecord) {

            if ($this->record->isChinese()) {
                $select[] = 'cn_medication';
            }

            $medication = MedicalDispose::select($select)->where(['record_id' => $this->record->id])->first();
        }

        return $this->item($medication, function ($medication) use ($select) {
            if (empty($medication)) {
                return ['medication'=>'', 'cn_medication'=>''];
            }

            $back = [
                'medication'=> $medication->medication
            ];
            if (in_array('cn_medication', $select)) {
                $back['cn_medication'] = $medication->cn_medication;
            }

            return $back;
        });
    }
}